package com.fun_ordering.controller;

import com.fun_ordering.components.ApiResponse;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.pojo.FoPurchasing;
import com.fun_ordering.service.FoPurchasingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 进货表controller
 * @author XiaoPi
 *
 */
@RestController
@RequestMapping("/foPurchasing")
public class FoPurchasingController {

	@Autowired
	private FoPurchasingService foPurchasingService;

	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<FoPurchasing> findAll(){
		return foPurchasingService.findAll();
	}


	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult  findPage(int page,int rows){
		return foPurchasingService.findPage(page, rows);
	}

	/**
	 * 增加
	 * @param foPurchasing
	 * @return
	 */
	@RequestMapping("/add")
	public ApiResponse add(@RequestBody FoPurchasing foPurchasing){
		try {
			String enterTime = foPurchasing.getEnterTime();
			OffsetDateTime inputDatetime = OffsetDateTime.parse(enterTime);
			DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
			String outputDatetimeStr = inputDatetime.format(formatter);
			foPurchasing.setEnterTime(outputDatetimeStr);
			System.out.println(outputDatetimeStr);
			foPurchasingService.add(foPurchasing);
		return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
		return ApiResponse.failed(null);
		}
	}

	/**
	 * 修改
	 * @param foPurchasing
	 * @return
	 */
	@RequestMapping("/update")
	public ApiResponse update(@RequestBody FoPurchasing foPurchasing){
		try {
			foPurchasingService.update(foPurchasing);
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

	/**
	 * 获取实体
	 * @param purId
	 * @return
	 */
	@RequestMapping("/findOne")
	public FoPurchasing findOne(Integer purId){
		return foPurchasingService.findOne(purId);
	}

	/**
	 * 批量删除
	 * @param purIds
	 * @return
	 */
	@RequestMapping("/delete")
	public ApiResponse delete(Integer [] purIds){
		try {
			foPurchasingService.delete(purIds);
			return ApiResponse.success(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResponse.failed(null);
		}
	}

		/**
	 * 查询+分页
	 * @param foPurchasing
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/pageSearch")
	public PageResult search(@RequestBody FoPurchasing foPurchasing, int page, int rows  ){
		return foPurchasingService.findPage(foPurchasing, page, rows);
	}

}
